/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */
using System;
using System.Xml;
using System.Collections;

namespace Pbdms.BusinessLogic.RSS
{
	/// <summary>
	/// RSS version 2.0 reader
	/// </summary>
	public class Rss20Reader {
		
		public Rss20Reader() {
		}

		public bool canRead(XmlDocument document) {
			XmlNode versionAttr = document.DocumentElement.Attributes.GetNamedItem("version");
			string defaultNamespace = document.DocumentElement.GetNamespaceOfPrefix("");

			if (versionAttr != null && versionAttr.InnerText == "2.0" && defaultNamespace == "") {
				// Check if it's a radioland 2.0 format (improvements to the indentification is welcome)
				XmlNode skipHoursNode = document.DocumentElement.SelectSingleNode("//channel/skipHours");
				XmlNode managingEditorNode = document.DocumentElement.SelectSingleNode("//channel/managingEditor");
				
                /*
                 *
                if (skipHoursNode == null && managingEditorNode == null) {
					return true;
				} else {
					return false;
				}
                 * */
                
                return true;
			} else {
				return false;
			}
		}

		public IDictionary readFeeds(XmlDocument document) {
			XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
			nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
			nsmgr.AddNamespace("sy", "http://purl.org/rss/1.0/modules/syndication/");
			nsmgr.AddNamespace("admin", "http://webns.net/mvcb/");
			nsmgr.AddNamespace("rdf" ,"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
			nsmgr.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");

			XmlNodeList itemList = document.SelectNodes("//rss/channel/item");
			IDictionary returnValue = new Hashtable(itemList.Count);
			foreach (XmlNode item in itemList) {
				FeedEntry entry = new FeedEntry();
				entry.title = XmlUtils.getNodeText(item, "title");
				entry.description = XmlUtils.getNodeText(item, "description");
				entry.link = XmlUtils.getNodeText(item, "link");
                entry.publicationDate = XmlUtils.getNodeDateTime(item, "pubDate", nsmgr).ToUniversalTime();
				entry.subject = XmlUtils.getNodeText(item , "dc:subject", nsmgr);
				XmlNode guidNode = item.SelectSingleNode("guid");
				if (guidNode != null) {
					entry.rssGuid.guid = guidNode.InnerText;
					entry.rssGuid.isPermaLink = XmlUtils.getAttributeBoolValue(guidNode, "isPermaLink", "");
				} else {
					
				}
                try
                {
                    returnValue.Add(entry.cacheKey, entry);
                }
                catch
                {
                }
			}
			return returnValue;
		}

		public ChannelInformation readInformation(XmlDocument document) {
			XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
			nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
			nsmgr.AddNamespace("sy", "http://purl.org/rss/1.0/modules/syndication/");
			nsmgr.AddNamespace("admin", "http://webns.net/mvcb/");
			nsmgr.AddNamespace("rdf" ,"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
			nsmgr.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");

			ChannelInformation chInfo = new ChannelInformation();
			XmlElement root = document.DocumentElement;
			
			chInfo.title = XmlUtils.getNodeText(root, "//title");
			chInfo.link = XmlUtils.getNodeText(root, "//link");
			chInfo.description = XmlUtils.getNodeText(root, "//description");
			chInfo.language = XmlUtils.getNodeText(root, "//dc:language", nsmgr);
			chInfo.creator = XmlUtils.getNodeText(root, "//dc:creator", nsmgr);
			chInfo.rights = XmlUtils.getNodeText(root, "//dc:rights", nsmgr);
			chInfo.date = XmlUtils.getNodeDateTime(root, "//dc:date", nsmgr);
			
			XmlNode xmlNode = root.SelectSingleNode("//admin:generatorAgent", nsmgr);
			chInfo.generatorAgent = XmlUtils.getAttributeValue(xmlNode, "resource", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
			xmlNode = root.SelectSingleNode("//admin:errorReportsTo", nsmgr);
			chInfo.errorReportsTo = XmlUtils.getAttributeValue(xmlNode, "resource", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
			
			string updatePeriod = XmlUtils.getNodeText(root, "//sy:updatePeriod", nsmgr);
			chInfo.updatePeriod = ChannelInformation.updatePeriodStringToEnum(updatePeriod);
			try {
				chInfo.updateFrequency = XmlUtils.getNodeIntegerValue(root, "//sy:updateFrequency", nsmgr);
			} catch (Exception e) {
				
				// Set a default value that we like
				chInfo.updateFrequency = 15;
			}
			
			chInfo.updateBase = XmlUtils.getNodeDateTime(root, "//sy:updateBase", nsmgr);

			
			return chInfo;
		}

	}
}
